/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.wx.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeesite.common.lang.DateUtils;
import com.jeesite.common.utils.excel.ExcelExport;
import com.jeesite.modules.common.refun.CollectionUtil;
import com.jeesite.modules.common.refun.StringUtil;
import com.jeesite.modules.sys.entity.EmpUser;
import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.wx.entity.WxUserDisValueRecord;
import com.jeesite.modules.wx.service.WxUserDisValueRecordService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.wx.entity.WxUser;
import com.jeesite.modules.wx.service.WxUserService;

import java.math.BigDecimal;
import java.util.List;

/**
 * 用户表Controller
 * @author wangjianxin
 * @version 2018-09-27
 */
@Controller
@RequestMapping(value = "${adminPath}/wx/wxUser")
public class WxUserController extends BaseController {

	@Autowired
	private WxUserService wxUserService;
	@Autowired
	private WxUserDisValueRecordService wxUserDisValueRecordService;

	private volatile String globalActivityId;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public WxUser get(String id, boolean isNewRecord) {
		return wxUserService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("wx:wxUser:view")
	@RequestMapping(value = {"list", ""})
	public String list(WxUser wxUser, Model model) {
		model.addAttribute("wxUser", wxUser);
		return "modules/wx/wxUserList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("wx:wxUser:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<WxUser> listData(WxUser wxUser, HttpServletRequest request, HttpServletResponse response,
								 @RequestParam(value = "activityId",required = false) String activityId){
		wxUser.setAttendActivityIds(activityId);
		Page<WxUser> page = wxUserService.findPage(new Page<WxUser>(request, response), wxUser);
		List<WxUser> wxUserList = page.getList();
		if(CollectionUtil.isNotEmpty(wxUserList)){
			for(WxUser temp : wxUserList){
				temp.setAttendActivityIds(activityId);

				logger.info(temp.getDisMoneyValue());
				String money = temp.getDisMoneyValue();
				if(StringUtil.isEmpty(money)){
					temp.setDisMoneyValue(BigDecimal.ZERO.toString());
				}
				//总返佣
//				WxUserDisValueRecord wxUserDisValueRecord = new WxUserDisValueRecord();
//				wxUserDisValueRecord.setBuyActivityId(Long.valueOf(activityId));
//				wxUserDisValueRecord.setUserOpenId(temp.getWxOpenId());
//				wxUserDisValueRecord.setBuyFlag(1);
//				List<WxUserDisValueRecord> wxUserDisValueRecordList = wxUserDisValueRecordService.findList(wxUserDisValueRecord);
//				BigDecimal disAmount = BigDecimal.ZERO;
//				Integer disTimes = 0;
//				if(CollectionUtil.isNotEmpty(wxUserDisValueRecordList)){
//
//					for(WxUserDisValueRecord moneyList:wxUserDisValueRecordList){
//						if(null != moneyList.getDisMoneyValue()){
//							disAmount = disAmount.add(moneyList.getDisMoneyValue());
//							++disTimes;
//						}
//					}
//				}
//				temp.setDisInviteNum(disTimes.toString());
			}
		}
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("wx:wxUser:view")
	@RequestMapping(value = "form")
	public String form(WxUser wxUser, Model model) {
		model.addAttribute("wxUser", wxUser);
		return "modules/wx/wxUserForm";
	}

	/**
	 * 保存用户表
	 */
	@RequiresPermissions("wx:wxUser:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated WxUser wxUser) {
		wxUserService.save(wxUser);
		return renderResult(Global.TRUE, text("保存用户成功！"));
	}
	
	/**
	 * 删除用户表
	 */
	@RequiresPermissions("wx:wxUser:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(WxUser wxUser) {
		wxUserService.delete(wxUser);
		return renderResult(Global.TRUE, text("删除用户成功！"));
	}

	/**
	 * 导出数据
	 * @param wxUser
	 * @param isAll
	 * @param response
	 */
	@RequestMapping({"exportData"})
	@ResponseBody
	public void exportData(WxUser wxUser, Boolean isAll, HttpServletResponse response,
						   @RequestParam(value = "exportActivityId",required = false) String exportActivityId) {
		logger.info("exportData activityId={}",exportActivityId);

		wxUser.setAttendActivityIds(exportActivityId);
		List<WxUser> list = this.wxUserService.findList(wxUser);
		String fileName = "用户数据" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
		ExcelExport ee = new ExcelExport("用户数据", WxUser.class);
		Throwable var7 = null;

		try {
			ee.setDataList(list).write(response, fileName);
		} catch (Throwable var16) {
			var7 = var16;
			throw var16;
		} finally {
			if (ee != null) {
				if (var7 != null) {
					try {
						ee.close();
					} catch (Throwable var15) {
						var7.addSuppressed(var15);
					}
				} else {
					ee.close();
				}
			}

		}

	}
	
}